const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const md5 = require("blueimp-md5");
const http = require('http');
let fileName = "";
request('https://chejiahao.autohome.com.cn/info/4515345#pvareaid=6826274',function(err,response,body){
   console.log('正在爬取资源...');
    const $ = cheerio.load(body,{decodeEntities: false});
    const articleList = $('#gallery-selector .ahe__area');
    fileName = $('title').text();
    let allData = [];
    articleList.each(function(){
        let role = $(this).attr('ahe-role');
        switch (role){
            case 'text':
                let textDomArr = $(this).find('p');
                let textArr = [];
                textDomArr.each(function(){
                    textArr.push($(this).text());
                });
                allData.push({
                    type:"text",
                    content:textArr.toString()
                });
                break;
            case 'image':
                let imgDomArr = $(this).find('img');
                imgDomArr.each(function(){
                    let originImgUrl = $(this).attr('data-imageurl');                    
                    allData.push({
                        type:"image",
                        content:originImgUrl
                    });
                });
                break;
        }
    });
    Promise.all(downloadImg(allData)).then(res=>{
        upload(res);
    }).catch(err=>{
        console.log(err);
    });
});
function upload(res){
    let newData =  res.map((data,index)=>{
        return new Promise((resolve,reject)=>{
                if(data.type=="image"){
                    setTimeout(() => {
                        let uts = Date.now();
                        let formData = {
                            uts:uts,
                            sign:md5(uts),
                            type:0,
                            'token':'gAUlfWPoktfifNe7jwvj8sE0ErDOUwyZbGAGkhq7r4GFoofLD2NYFvpe1511kvAZrLo1Hb4OygIo5Vz1xgybRaT21LlH3AcpXBqPWGEUIW3fc5nvjgXkL7jbED7jc+MAS6R73d9xfZEVfZMbOlY5HVmAKusaZ7XFZ2UvkK7IeCg=',
                            file: fs.createReadStream(`${__dirname}${data['content']}`)
                        }
                        request.post({url:'https://test2.7yueye.com/Upload/UploadImg', formData: formData},function(err,response,body){
                            if(err){
                                reject();
                            }else{
                                let result = JSON.parse(body);
                                data['content'] = result['data'][0]['url'];
                                console.log(`第${index}张图片上传完成`);
                                resolve(data);
                            }
                        }) 
                    });
                }else{
                    resolve(data);
                }
        });
    });
    Promise.all(newData).then(res=>{
        fs.writeFile(`./articles/${fileName}.json`,JSON.stringify(res),err=>{
            if(err){
                console.log(err);
            }else{
                console.log('文件写入成功');
            }
        })
    }).then(res=>{
        unlinkImg();
    }).catch(err=>{
        console.log(err);
    });
}
function downloadImg(data){
    let newData = data.map((item,index)=>{
        return new Promise((resolve,reject)=>{
            if(item.type=='image'){
                let originImgUrl = item.content;
                let suffix = originImgUrl.split('.').pop();
                let imgName = `${md5(originImgUrl)}.${suffix}`;
                http.get(originImgUrl,function(res){
                    var imgData = "";
                    res.setEncoding("binary");
                    res.on("data", function (chunk) {
                        imgData += chunk;
                    });
                    res.on("end", function () {
                        fs.writeFile(`./imgs/${imgName}`, imgData, "binary", function (err) {
                            if (err) {
                                reject(err);
                            }
                            item.content = `/imgs/${imgName}`;
                            console.log(`第${index}张图片下载完成`);
                            resolve(item);
                        });
                    });
                    res.on("error", function (err) {
                        reject(err);
                    });
                });
            }else{
                resolve(item);
            }
        });
    });
    return newData;
}

function unlinkImg(){
    const fileUrl = __dirname+'/imgs'
    let files = fs.readdirSync(fileUrl);
    files.forEach(function(file){
        let stats = fs.statSync(fileUrl+'/'+file);
        if(!stats.isDirectory()){
            fs.unlinkSync(fileUrl+'/'+file); 
            console.log("删除文件"+fileUrl+'/'+file+"成功");
        }
    });
}